﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NEXEntidad;
using System.Threading;

public partial class WebControls_BoxEditoriales : System.Web.UI.UserControl
{

    #region Campos / Propiedades
    private int fIdLibro;
    public int IdLibro
    {
        get { return fIdLibro; }
        set { fIdLibro = value; }
    }

    #endregion

    #region Eventos de Pagina
    protected void Page_Load(object sender, EventArgs e)
    {
        int idLibro;
        EditorialCollection editoriales;
        if (!IsPostBack)
        {
            idLibro = fIdLibro;//Convert.ToInt32(Request.QueryString["idLibro"].ToString());
            editoriales = Editorial.ListarPorLibro(idLibro, false);
            PaginarEditoriales(1, editoriales);
        }
    }

    protected void repeaterEditorial_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Editorial unaEditorial;
        try
        {
            unaEditorial = (Editorial)e.Item.DataItem;

            ((Label)e.Item.FindControl("lblNombre")).Text = unaEditorial.Nombre;
            ((Label)e.Item.FindControl("lblIsbn")).Text = unaEditorial.Isbn;
            ((Label)e.Item.FindControl("lblAnio")).Text = unaEditorial.Anio.ToString();
            ((Label)e.Item.FindControl("lblPaginas")).Text = unaEditorial.Paginas.ToString();
            ((Label)e.Item.FindControl("lblInfoEditorial")).Text = unaEditorial.InfoEditorial;
            if (unaEditorial.NombreFileImagen != "")
            {
                ((Image)e.Item.FindControl("imgFoto")).ImageUrl = @"../tapaLibros/" + unaEditorial.NombreFileImagen;
            }
            else
            {
                ((Image)e.Item.FindControl("imgFoto")).ImageUrl = @"../images/" + "img_silueta_libros.jpg";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    protected void btnFlechas_Click(object sender, ImageClickEventArgs e)
    {
        int idLibro;
        EditorialCollection editoriales;
        int CurPage;
        try
        {
            Thread.Sleep(1000);
            idLibro = Convert.ToInt32(Request.QueryString["idLibro"].ToString());
            editoriales = Editorial.ListarPorLibro(idLibro, false);
            if (((ImageButton)sender).Attributes["Pagina"] != null)
                CurPage = Convert.ToInt32(((ImageButton)sender).Attributes["pagina"]);
            else
                CurPage = 1;

            PaginarEditoriales(CurPage, editoriales);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion

    #region Metodos Privados
    protected void PaginarEditoriales(int PaginaActual, EditorialCollection editoriales)
    {
        PagedDataSource pdDataSource = new PagedDataSource();


        pdDataSource.DataSource = editoriales;
        pdDataSource.AllowPaging = true;
        pdDataSource.PageSize = 1;

        pdDataSource.CurrentPageIndex = PaginaActual - 1;

        repeaterEditorial.DataSource = pdDataSource;
        repeaterEditorial.DataBind();

        if (!pdDataSource.IsFirstPage)
        {
            btnAnterior.Attributes["Pagina"] = Convert.ToString(PaginaActual - 1);
            btnAnterior.Visible = true;
        }
        else
            btnAnterior.Visible = false;

        if (pdDataSource.PageCount > 0)
            if (pdDataSource.PageCount > 1)
            {
                lblPaginas.Visible = true;
                lblPaginas.Text = "(Pag. " + PaginaActual.ToString() + " de " + pdDataSource.PageCount.ToString() + ")";
                if (!pdDataSource.IsLastPage)
                {
                    btnSiguiente.Visible = true;
                    btnSiguiente.Attributes["Pagina"] = Convert.ToString(PaginaActual + 1);
                }
                else
                    btnSiguiente.Visible = false;
            }
            else
            {
                lblPaginas.Visible = false;
                lblPaginas.Text = "";
            }

    }
    #endregion

}
